"""
客户相关模型
"""
from tortoise import fields
from .base import BaseModel


class Customer(BaseModel):
    """客户模型"""
    
    # 基本信息
    name = fields.CharField(max_length=200, description="客户名称")
    code = fields.CharField(max_length=100, unique=True, description="客户编码")
    customer_type = fields.CharField(
        max_length=20,
        default="individual",
        description="客户类型: individual-个人, enterprise-企业, government-政府"
    )
    
    # 联系信息
    contact_person = fields.CharField(max_length=100, null=True, description="联系人")
    phone = fields.CharField(max_length=20, null=True, description="电话")
    email = fields.CharField(max_length=100, null=True, description="邮箱")
    address = fields.TextField(null=True, description="地址")
    
    # 企业信息
    company_name = fields.CharField(max_length=200, null=True, description="公司名称")
    business_license = fields.CharField(max_length=100, null=True, description="营业执照号")
    tax_number = fields.CharField(max_length=100, null=True, description="税号")
    
    # 信用信息
    credit_level = fields.CharField(
        max_length=10,
        default="A",
        description="信用等级: AAA, AA, A, B, C, D"
    )
    credit_limit = fields.DecimalField(max_digits=12, decimal_places=2, null=True, description="信用额度")
    
    # 状态信息
    status = fields.CharField(
        max_length=20,
        default="active",
        description="状态: active-活跃, inactive-非活跃, blacklist-黑名单"
    )
    
    # 其他信息
    description = fields.TextField(null=True, description="客户描述")
    notes = fields.TextField(null=True, description="备注")
    
    class Meta:
        table = "customers"
        table_description = "客户表"


class CustomerContact(BaseModel):
    """客户联系人模型"""
    
    customer = fields.ForeignKeyField("models.Customer", related_name="contacts", description="客户")
    name = fields.CharField(max_length=100, description="联系人姓名")
    position = fields.CharField(max_length=100, null=True, description="职位")
    phone = fields.CharField(max_length=20, null=True, description="电话")
    email = fields.CharField(max_length=100, null=True, description="邮箱")
    is_primary = fields.BooleanField(default=False, description="是否主要联系人")
    
    class Meta:
        table = "customer_contacts"
        table_description = "客户联系人表"


class CustomerAddress(BaseModel):
    """客户地址模型"""
    
    customer = fields.ForeignKeyField("models.Customer", related_name="addresses", description="客户")
    address_type = fields.CharField(
        max_length=20,
        default="delivery",
        description="地址类型: delivery-收货地址, billing-账单地址, office-办公地址"
    )
    country = fields.CharField(max_length=100, default="中国", description="国家")
    province = fields.CharField(max_length=100, description="省份")
    city = fields.CharField(max_length=100, description="城市")
    district = fields.CharField(max_length=100, null=True, description="区县")
    street = fields.CharField(max_length=200, description="街道地址")
    postal_code = fields.CharField(max_length=20, null=True, description="邮政编码")
    latitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="纬度")
    longitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="经度")
    is_default = fields.BooleanField(default=False, description="是否默认地址")
    
    class Meta:
        table = "customer_addresses"
        table_description = "客户地址表"
